package com.xksms.miniotest.controller;

import com.xksms.miniotest.utils.text.JsonResult;
import com.xksms.miniotest.service.UploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName FileController
 * @Description 文件上传接口
 * @Author 程超
 * @Date 2022/7/27 17:30
 */
@Tag(name = "文件上传接口")
@RequestMapping("/file")
@RestController
public class UploadController {

    private final UploadService uploadService;

    public UploadController(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    /**
     * @param file             文件
     * @param bucket           桶名
     * @param objectName       路径
     * @param quality          压缩质量 1为百分之百
     * @param positions        水印位置
     * @param waterMarkContent 水印内容   换行  以 \ 为分隔符
     * @param deductReason     扣分标准 也是文件名
     * @param rgb              水印颜色
     * @param alpha            水印透明度
     * @return com.ance.common.utils.JsonResult
     * @Author chengchao
     * @Description
     * @Date 15:48 2022/8/3
     **/
    @Operation(summary = "测评图片 minio上传一个水印照片")
    @PostMapping("/uploadWaterMarkImage")
    public JsonResult uploadWaterMarkImage(@RequestPart("file") MultipartFile file, @RequestParam("bucket") String bucket,
                                           @RequestParam(value = "objectName", defaultValue = "") String objectName, @RequestParam(value = "quality", defaultValue = "0.4") float quality,
                                           @RequestParam(value = "positions", defaultValue = "BOTTOM_LEFT") String positions,
                                           @RequestParam(value = "waterMarkContent", required = false) String waterMarkContent,
                                           @RequestParam("deductReason") String deductReason,
                                           @RequestParam(value = "rgb", defaultValue = "000000") String rgb,
                                           @RequestParam(value = "alpha", defaultValue = "0.8") float alpha) throws Exception {
        return uploadService.uploadWaterMarkImage(file, bucket, objectName, quality, positions, waterMarkContent, deductReason, rgb, alpha);
    }

    /**
     * @param files      文件
     * @param bucket     桶名
     * @param objectName 路径
     * @return com.xksms.miniotest.Utils.text.JsonResult
     * @Author chengchao
     * @Description 上传多个文件  问题是swagger 不能无法上传多个文件(应该是我个人原因)  建议使用 postman的软件
     * @Date 15:59 2022/8/4
     **/
    @Operation(summary = "minio上传文件")
    @PostMapping("/uploadFiles")
    public JsonResult uploadFiles(@RequestParam("files") MultipartFile[] files, @RequestParam("bucket") String bucket,
                                  @RequestParam(value = "objectName", defaultValue = "") String objectName) throws Exception {
        return uploadService.uploadFiles(files, bucket, objectName);
    }

    /**
     * description:  minio下载文件 通过url下载文件
     *
     * @param url      文件url
     * @param response 响应
     * @return com.xksms.miniotest.utils.text.JsonResult
     * @throws IOException 异常
     * @author chengchao
     * @date 2022/8/5 11:03
     */
    @Operation(summary = "minio下载文件")
    @PostMapping("/downloadFile")
    public JsonResult downloadFile(@RequestParam("url") String url, HttpServletResponse response) {
        return uploadService.downloadFile(url, response);
    }

    /**
     * description:  删除一个文件
     * @param url 文件url
     * @return com.xksms.miniotest.utils.text.JsonResult
     * @author chengchao
     * @date  2022/8/5 14:43
     */
    @Operation(summary = "minio删除单个文件")
    @PostMapping("/deleteFile")
    public JsonResult deleteFile(@RequestParam("url") String url) {
        return uploadService.deleteFile(url);
    }

    /**
     * description:  删除多个文件
     * @param urls  文件url的数组
     * @return com.xksms.miniotest.utils.text.JsonResult
     * @throws  Exception exception
     * @author ChengChao
     * @date  2022/8/5 17:21
     */
    @Operation(summary = "minio删除多个文件")
    @PostMapping("/deleteFiles")
    public JsonResult deleteFiles(@RequestBody String[] urls)  throws Exception {
        return uploadService.deleteFiles(urls);
    }

    /**
     * description:   多文件上传同时支持重命名
     * @param files 文件
     * @param bucket 桶名
     * @param objectName 路径
     * @param rename  重命名
     * @return java.util.List<java.lang.String>
     * @author ChengChao
     * @date  2022/8/9 17:22
     */
    //重命名上传多文件
    @Operation(summary = "minio重命名上传多文件")
    @PostMapping("/uploadFilesRename")
    public JsonResult uploadFilesRename(@RequestParam("files") MultipartFile[] files, @RequestParam("bucket") String bucket,
                                  @RequestParam(value = "objectName", defaultValue = "") String objectName,
                                  @RequestParam(value = "rename",required = false)String rename) {
        return uploadService.uploadFilesRename(files, bucket, objectName,rename);
    }

    //获得桶的配置信息
    @Operation(summary = "minio获得桶的配置信息")
    @PostMapping("/getBucketConfig")
    public JsonResult getBucketConfig(@RequestParam("bucket") String bucket) {
        return uploadService.getBucketConfig(bucket);
    }
    //更改桶的配置信息
    @Operation(summary = "minio更改桶的配置信息")
    @PostMapping("/setBucketConfig")
    public JsonResult setBucketConfig(@RequestParam("bucket") String bucket) {
        return uploadService.setBucketConfig(bucket);
    }

    //直接打水印
    @Operation(summary = "minio直接打水印")
    @PostMapping("/uploadMarkImage")
    public JsonResult uploadMarkImage(@RequestParam("file") MultipartFile file, @RequestParam("bucket") String bucket,
                                           @RequestParam(value = "objectName", defaultValue = "") String objectName,
                                           @RequestParam(value = "waterMarkContent", required = false) String waterMarkContent) throws Exception {
        return uploadService.uploadMarkImage(file, bucket, objectName, waterMarkContent);
    }
}
